package com.uyayo.system.config.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.framework.common.exception.CodeMultipleException;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import com.uyayo.system.config.entity.SysPermissionInit;
import com.uyayo.system.config.mapper.SysPermissionInitMapper;
import com.uyayo.system.config.model.SysPermissionInitCondition;
import com.uyayo.system.config.service.SysPermissionInitService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service("sysPermissionInitService")
public class SysPermissionInitServiceImpl implements SysPermissionInitService {

	@Resource
	private SysPermissionInitMapper sysPermissionInitMapper;

	@Override
	public SysPermissionInit getSysPermissionInitById(long id){
		return sysPermissionInitMapper.selectByPrimaryKey(id);
	}

	@Override
	public PageBean<SysPermissionInit> selectSysPermissionInit(PageBean<SysPermissionInit> pageBean,SysPermissionInitCondition sysPermissionInitCondition) {
		
		List<SysPermissionInit> sysPermissionInit;
		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("code", "code"));
		orderBies.add(new OrderBy("name", "name"));
		orderBies.add(new OrderBy("description", "description"));
		orderBies.add(new OrderBy("createTime", "create_time"));
		orderBies.add(new OrderBy("updateTime", "update_time"));
		orderBies.add(new OrderBy("remark", "remark"));
		OrderByUtil orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, pageBean.getOrderField(), pageBean.getOrderDirection());
		PageHelper.startPage(pageBean.getPageCurrent(), pageBean.getPageSize());
		Example example = new Example(SysPermissionInit.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(sysPermissionInitCondition.getUrl())){
			criteria.andLike("url","%"+sysPermissionInitCondition.getUrl()+"%");
		}
		if(sysPermissionInitCondition.getBeginCreateTime()!=null || sysPermissionInitCondition.getEndCreateTime()!=null){
			criteria.andBetween("createTime", sysPermissionInitCondition.getBeginCreateTime(), sysPermissionInitCondition.getEndCreateTime());
		}
		sysPermissionInit = sysPermissionInitMapper.selectByExample(example);
		
		//取记录总条数
		PageInfo<SysPermissionInit> pageInfo = new PageInfo<>(sysPermissionInit);
		
		return new PageBean<>(pageInfo.getTotal(), pageBean.getPageCurrent(), pageBean.getPageSize(), sysPermissionInit);
	}

	@Override
	public List<SysPermissionInit> queryAll(SysPermissionInit sysPermissionInit){
		return sysPermissionInitMapper.queryAll(sysPermissionInit);
	}

	@Override
	public List<SysPermissionInit> selectSysPermissionInit(SysPermissionInit sysPermissionInit) {
		return sysPermissionInitMapper.select(sysPermissionInit);
	}

	@Override
	public int saveSysPermissionInit(SysPermissionInit sysPermissionInit) throws CodeMultipleException {
        SysPermissionInit sysParamTest = getSysPermissionInitByCode(sysPermissionInit.getCode());
        if(sysParamTest!=null){
            throw new CodeMultipleException("Code multiple error!");
        }
		return sysPermissionInitMapper.insertSelective(sysPermissionInit);
	}

	@Override
	public int updateSysPermissionInit(SysPermissionInit sysPermissionInit) throws CodeMultipleException {
		SysPermissionInit sysSysPermissionInitTest = getSysPermissionInitByCode(sysPermissionInit.getCode());
		if(sysSysPermissionInitTest!=null && !Objects.equals(sysPermissionInit.getId(),sysSysPermissionInitTest.getId())){
			throw new CodeMultipleException("Code multiple error!");
		}
		return sysPermissionInitMapper.updateByPrimaryKeySelective(sysPermissionInit);
	}

	@Override
	public int deleteSysPermissionInitById(Long id) {
		return sysPermissionInitMapper.deleteByPrimaryKey(id);
	}

	@Override
	public SysPermissionInit getSysPermissionInitByCode(String code) {
		SysPermissionInit sysPermissionInit = new SysPermissionInit();
		sysPermissionInit.setCode(code);
		sysPermissionInit = sysPermissionInitMapper.selectOne(sysPermissionInit);
		return sysPermissionInit;
	}

	@Override
	public SysPermissionInit getSysPermissionInitByEnabled(){
		SysPermissionInit sysPermissionInit = new SysPermissionInit();
		sysPermissionInit.setStatus(1);
		List<SysPermissionInit> Servers =  sysPermissionInitMapper.select(sysPermissionInit);
		return Servers.get(0);
	}
}
